import { START, SUCCESS, FAIL, AllAction } from './action'

interface List {
	title: string
	id: number
	userId: number
	completed: boolean
}
interface State {
	loading: boolean
	error: string | null
	productList: Array<List>
}

const defaultState: State = {
	loading: false,
	error: null,
	productList: []
}

export default (state = defaultState, action: AllAction): State => {
	switch (action.type) {
		case START:
			return { ...state, loading: true }
		case SUCCESS:
			return { ...state, loading: false, productList: action.payload?.slice(0, 10) }
		case FAIL:
			return { ...state, loading: false, error: action.payload }
		default:
			return state
	}
}
